/**
  * <br>JAVACC DEMO 1.0<br>
  * @copy right dwu company All rights reserved.<br>
  * <br>
  * @Package com.apache.rpc.server
*/
package com.apache.rpc.server;

import jxl.common.Logger;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;

import com.apache.database.constant.SystemTools;
import com.apache.database.constant.Validator;
import com.apache.method.ThreadPool;
import com.apache.rpc.client.ProtocolServiceClient;
import com.apache.rpc.server.impl.ProtocolEncryptionServer;
import com.apache.rpc.server.impl.ProtocolServer;
import com.apache.rpc.service.impl.helper.CacheTactics;

/**
  * description: 系统启动初始化初始化socket服务
  * @author Hou DaYu 创建时间：下午02:14:42
  */
public class ProtocolListener implements ApplicationListener<ApplicationEvent> {

	private Logger log = Logger.getLogger(ApplicationListener.class);
	public static boolean isStart = false;

	private String openServer;
	private String portStr;
	private String encryProtStr;
	private CacheTactics interfaceRegisterCacheTactics;

	@Override
	public void onApplicationEvent(ApplicationEvent event) {
		if (isStart)
			return;

		if (event instanceof ContextClosedEvent) {//当ApplicationContext关闭时触发该事件

		} else if (event instanceof ContextRefreshedEvent) {//当ApplicationContext初始化或者刷新时触发该事件
			if (event.getTimestamp() > 0) {

				if ("1".equals(SystemTools.getInstance().getValue("rpc_initcache"))) {
					//					CacheTactics cacheTactics = (CacheTactics) SpringContextLoader
					//							.getBean("interfaceRegisterCacheTactics");
					interfaceRegisterCacheTactics.initCaches();//初始化接口注册信息
				} else {
					log.warn("RPC-Warn:RPC缓存没有开启");
				}
				openServer = Validator.getDefaultStr(SystemTools.getInstance().getValue("openSocketServer"), "0");
				if ("1".equals(openServer)) {
					portStr = Validator.getDefaultStr(SystemTools.getInstance().getValue("rpc_service_port"), "9001");
					ProtocolServer run = new ProtocolServer(openServer, portStr);
					ThreadPool taskPool = new ThreadPool(1, "socket server执行线程池");
					taskPool.execute(run);//非加密socket服务
					encryProtStr = Validator.getDefaultStr(SystemTools.getInstance().getValue("socket_encry_port"),
							"9002");
					ProtocolEncryptionServer eserver = new ProtocolEncryptionServer(encryProtStr);
					ThreadPool taskEncryPool = new ThreadPool(1, "socket encryption server执行线程池");
					taskEncryPool.execute(eserver);//加密socket服务
				}
				ProtocolServiceClient.instance();
			}
		}
		isStart = true;
	}

	//	public void setOpenServer(String openServer) {
	//		this.openServer = openServer;
	//	}
	//
	//	public void setPortStr(String portStr) {
	//		this.portStr = portStr;
	//	}
	//
	//	public void setEncryProtStr(String encryProtStr) {
	//		this.encryProtStr = encryProtStr;
	//	}

	public void setInterfaceRegisterCacheTactics(CacheTactics interfaceRegisterCacheTactics) {
		this.interfaceRegisterCacheTactics = interfaceRegisterCacheTactics;
	}
}
